package com.inspur.myapplication.view;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.inspur.myapplication.R;
import com.inspur.myapplication.dao.GoodsDao;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.ArrayList;
import java.util.HashMap;

public class GoodListTestActivity extends AppCompatActivity {
    ArrayList goodsList;
    ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //获取其他界面传递的数据
        Intent intent=getIntent();
        String category=intent.getStringExtra("category");
        Log.i(GoodListTestActivity.class.toString(),category);


        //准备数据
        goodsList=new ArrayList();
        //每个商品使用HashMap对象表示
        /*
        HashMap hashMap=new HashMap();
        hashMap.put("goodsName","goods1");
        hashMap.put("goodsPrice","20");
        hashMap.put("goodsImg",R.drawable.img);
        goodsList.add(hashMap);

        hashMap=new HashMap();
        hashMap.put("goodsName","goods2");
        hashMap.put("goodsPrice","21");
        hashMap.put("goodsImg",R.drawable.img);
        goodsList.add(hashMap);

        hashMap=new HashMap();
        hashMap.put("goodsName","goods3");
        hashMap.put("goodsPrice","22");
        hashMap.put("goodsImg",R.drawable.img);
        goodsList.add(hashMap);
        */
        GoodsDao goodsDao=new GoodsDao(this);
        goodsList=goodsDao.queryGoods(null,category);
        Log.i(GoodListTestActivity.class.toString(),goodsList.size()+"");

        final ImageLoader imageLoader=ImageLoader.getInstance();
        //初始化
        ImageLoaderConfiguration config=new ImageLoaderConfiguration.Builder(this).build();
        imageLoader.init(config);

        //使用SimpleAdapter，适用于图文并茂的效果
        SimpleAdapter simpleAdapter=new SimpleAdapter(this,goodsList,R.layout.layout_goods_item,new String[]{"goodsName","goodsPrice","goodsImg"},new int[]{R.id.goodsitem_name,R.id.goodsitem_price,R.id.goodsitem_iv});
        //通过SimpleAdapter的ViewBinder干预数据的显示
        simpleAdapter.setViewBinder(new SimpleAdapter.ViewBinder() {
            @Override
            public boolean setViewValue(View view, Object o, String s) {
                //setViewValue方法，对于每项数据，都会调用一次
                if(view.getId()==R.id.goodsitem_iv){
                    imageLoader.displayImage(o.toString(),(ImageView) view);
                    return true;//返回true,代表当前数据的展示已处理，不需要SimpleAdapter来负责
                }
                return false;//返回false,需要SimpleAdapter来负责展示
            }
        });

        setContentView(R.layout.layout_goodslist);

        listView=(ListView) findViewById(R.id.goodslist_lv);
        //给listView设置适配器
        listView.setAdapter(simpleAdapter);

        //设置监听器
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int index, long l) {
                HashMap hashMap1=(HashMap) goodsList.get(index);
                Toast.makeText(GoodListTestActivity.this,hashMap1.get("goodsName").toString(),Toast.LENGTH_LONG).show();
            }
        });


    }


}
